/**
 *  Copyright (C) 2010:
 *                 Buzzanca Marco <marco.bzn@gmail.com>
 *         Monaco Davide Giuseppe <black.ralkass@gmail.com>
 *              Petralia Veronica <veronicapetralia@gmail.com>
 *
 * This file is part of DS2011BMP
 *
 * DS2011BMP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DS2011BMP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * @file wfqueuemanagerproxy.h
 * @author Davide Giuseppe Monaco
 * @author Marco Buzzanca
 *
 * This header file contains WFQueueManager class declaration.
 */

#ifndef _WF_QUEUE_MANAGER_H_
#define _WF_QUEUE_MANAGER_H_ 1

#include <boost/bind.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <sstream>

#include "address.h"
#include "synque.h"
#include "udpinterface.h"
#include "udpserver.h"
#include "workflow.h"

/**
 * @brief WFQueueManager manages the Workflow queue for the Manager module and
 * handles communication with UI module.
 *
 * In order to receive enqueue() requests, WFQueueManager creates an UDPServer.
 */
class WFQueueManager
{
private:
  typedef boost::shared_ptr<char> buffer_pointer;

  /**
   * @brief WFQueueManager queue. It's a synchronized container.
   */
  synque<Workflow> _wfQueue;

  /**
   * @brief The UDP server that WFQueueManager will use in order to
   *        receive enqueue() requests.
   */
  UDPServer _server;

  /**
   * @brief WFQueueManager main thread callback member function.
   *
   * Listen for enqueue() requests and create a new worker thread
   * for its handling.
   */
  void run(void);

  /**
   * @brief WFQueueManager worker thread callback ember function.
   *
   * Deserialize buf obtaining a Workflow end push() it in the queue.
   *
   * @param buf   A pointer to received buffer containing a serialized Workflow.
   */
  void serve(buffer_pointer buf);

  /**
   * Main thread.
   */
  boost::shared_ptr<boost::thread> _s_thread;

  boost::mutex _m;

public:
  /**
   * @brief Default Constructor.
   *
   * @note This should not be used.
   */
  WFQueueManager(void);

  /**
   * @brief Constructor specifying server port. Default value is 12000.
   * @param port  Server port.
   */
  WFQueueManager(const Port& port = 12000);

  /**
   * @brief Constructor specifying server Address.
   * @param addr  Server Address.
   */
  WFQueueManager(const Address& addr);

  /**
   * @brief Destructor.
   */
  ~WFQueueManager(void);

  /**
   * @brief Gets server port.
   */
  boost::uint16_t getPort(void) const;

  /**
   * @brief Sets server port.
   */
  void setPort(boost::uint16_t port);

  /**
   * @brief Sets server port using provided Address.
   */
  void setPort(const Address& address);

  /**
   * @brief Push the provided Workflow into the queue.
   * @param   wf  The provided Workflow.
   * @return      Zero if operation successful, otherwise an error code.
   */
  int enqueue(const Workflow& wf);

  /**
   * @brief Pop from the queue the next Workflow and returns it.
   * @return      The next Workflow.
   */
   Workflow dequeue(void);

  /**
   * @brief Create the main thread and join on it.
   */
  void start(void);
};

#endif /* _WF_QUEUE_MANAGER_H_ */
